<?php

namespace app\index\controller;


use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\index\model\User as UserModel;

class User extends Controller
{
    public function index()
    {
        $user = Session::get('data');
        if (empty($user)) {
            return $this->redirect(url('/index/login'));
        }
        $list = $this->getUser();
        if (isset($list['fans'])) {
            foreach ($list['fans'] as $key => $fans) {
                $list['fans'][$key]['follow'] = 0;
                if (isset($list['follow'])) {
                    foreach ($list['follow'] as $follow) {
                    if ($fans['uid'] == $follow['uid']) {
                        $list['fans'][$key]['follow'] = 1;
                    }
                }
                }
                
            }
        }
        return $this->fetch('user/index', [
            'list' => $list,
        ]);
    }

    public function visitor($id)
    {
        $user = Session::get('data');
        if ($user['id'] == $id) {
            $this->redirect(url('index/User/index'));
        }
        $list = $this->getUser($id);
        $follow = Db::name('user_follow')->where('uid', $user['id'])->column('follow_id');
        $user_follow = Db::name('user_info')->where('uid', 'in', $follow)->field('uid')->select();
        if (isset($list['fans'])) {
            foreach ($list['fans'] as $key => $fans) {
                $list['fans'][$key]['fans'] = 0;
                if (!empty($user_follow)) {
                    foreach ($user_follow as $val) {
                    if ($fans['uid'] == $val['uid']) {
                        $list['fans'][$key]['fans'] = 1;
                    }
                }
            }
                }
                
        }
        if (isset($list['follow'])) {
            foreach ($list['follow'] as $key => $follow) {
                $list['follow'][$key]['follow'] = 0;
                if (!empty($user_follow)) {
                   foreach ($user_follow as $val) {
                    if ($follow['uid'] == $val['uid']) {
                        $list['follow'][$key]['follow'] = 1;
                    }
                }
            }
                }
                
        }
        $my_follow = false;
        foreach ($user_follow as $v) {
            if ($v['uid'] == $id) {
                $my_follow = true;
            }
        }
        return $this->fetch('user/visitor', [
            'list' => $list,
            'my_follow' =>$my_follow
        ]);
    }

    public function follow(Request $request,$id)
    {
        if (!$request->isAjax()) {
            return $this->error();
        }
        $uid = Session::get('data')['id'];
        if ($uid == $id) {
            return 3;
        }
        $data = [
            'uid' => $uid,
            'follow_id' => $id
        ];
        $list = Db::name('user_follow')->where($data)->find();
        if (empty($list)) {
            $result = Db::name('user_follow')->insert($data);
            if ($result == true) {
                Db::name('user_info')->where('uid',$uid)->setInc('follow_num');
                Db::name('user_info')->where('uid',$id)->setInc('fans_num');
                return 1;
            }
        } else {
            $result = Db::name('user_follow')->where($data)->delete();
            if ($result == true) {
                Db::name('user_info')->where('uid',$uid)->setDec('follow_num');
                Db::name('user_info')->where('uid',$id)->setDec('fans_num');
                return 2;
            }
        }
    }

    public function edit(Request $request)
    {
        $data = $request->post();
        $id = Session::get('data')['id'];
        $key = substr($data['name'],5);
        if (($data['value']) !== '') {
            if (is_array($data['value'])) {
                $list = implode(',',$data['value']);
            } else {
                $list = $data['value'];
            }
        } else {
            $list = '';
        }

        $list = [
            $key => $list
        ];

        $result = Db::name('user_info')->where('uid',$id)->update($list);
        return $result;
    }

    public function upload()
    {
        $file = request()->file('img');
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/user');
            if ($info) {
                echo $info->getSaveName();
            } else {
                echo $file->getError();
            }
        }
    }

    public function saveimg(Request $request)
    {
        $data = $request->post();
        $id = Session::get('data')['id'];
        $image = Db::name('user_img')->where('uid',$id)->find();
        if (empty($image)) {
            $list = ['image' => $data['image'], 'uid' => $id];
            $result = Db::name('user_img')->insert($list);
        } else {
            $result = Db::name('user_img')->where('uid',$id)->update($data);
        }
        $result = Db::name('user_info')->where('uid',$id)->update($data);
        return $result;
    }



    public function getUser($id = null)
    {
        $list = [];
        if (empty($id)) {
            $user = Session::get('data');
        } else {
            $user = Db::name('user')->where('id',$id)->field('id,tel,email')->find();
        }
        $list['user'] = $user;
        $info = Db::name('user_info')->where('uid', $user['id'])->field('nickname,sex,age,address,signature,interesting,note_num,fans_num,collection_num,follow_num,image')->find();
        $list['info'] = $info;
        $tag_name = Db::name('note_type')->where('id','in',$info['interesting'])->field('tag_name')->select();
        $list['info']['tag_name'] = $tag_name;
        if ($info['note_num'] > 0) {
            $note = Db::name('user_note')->order('id','desc')->where('uid', $user['id'])->column('id,title,thumb,create_at,praise', 'id');
            $list['note'] = $note;
        }
        if ($info['fans_num'] > 0) {
            $user_fans = Db::name('user_follow')->where('follow_id', $user['id'])->column('uid');
            $fans = Db::name('user_info')->where('uid', 'in', $user_fans)->field('uid,nickname,signature,image,sex')->select();
            $list['fans'] = $fans;
        }
        if ($info['follow_num'] > 0) {
            $user_follow = Db::name('user_follow')->where('uid', $user['id'])->column('follow_id');
            $follow = Db::name('user_info')->where('uid', 'in', $user_follow)->field('uid,nickname,signature,image,sex')->select();
            $list['follow'] = $follow;
        }
        if ($info['collection_num'] > 0) {
            $col = Db::name('user_collection')->where('uid', $user['id'])->find();
            $list['col'] = $col;
        }
        return $list;
    }
}